import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-06
 * Time: 01:21
 */
class MyArrayList<T>{
    private int[] array;
    private int usedSize;
    private static final int DefaultSize = 2;
    //无参构造方法
    public MyArrayList(){
        this.array = new int[DefaultSize];
    }
    //有参构造方法
    public MyArrayList(int initCapacity){
        this.array = new int[initCapacity];
    }
    //打印
    public void show(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.array[i]+" ");
        }
        System.out.println();
    }
    //判断是否满了
    public Boolean isFull(){
        if (this.array.length == this.usedSize)
            return true;
        else
            return false;
    }
    //从最后一个位置添加元素
    public void add(int data){
        if (isFull()){
            this.array = Arrays.copyOf(this.array,this.array.length*2);
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }
    //在pos位置添加元素
    public void add(int pos,int data){
        if (pos < 0 || pos > this.usedSize)
            throw new exception(pos+"位置不合法");
        if (isFull()){
            this.array = Arrays.copyOf(this.array,this.array.length*2);
        }
        for (int i = this.usedSize-1; i >= pos ; i--) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;
    }
    //判断某元素是否存在
    public boolean contains(int find){
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == find){
                return true;
            }
        }
        return false;
    }
    //找到目标元素下标
    public int indexOf(int find){
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == find){
                return i;
            }
        }
        return -1;
    }
    //判断输入下标是否合法
    public  Boolean check(int pos){
        if (pos < 0 || pos > this.usedSize - 1){
            return false;
        }
        return true;
    }
    //获取pos位置的元素
    public int get(int pos){
        if (!check(pos)){
            throw new exception(pos+"位置不合法");
        }
        return this.array[pos];
    }
    //将pos位置元素改为value
    public void set(int pos,int value){
        if (!check(pos)){
            throw new exception(pos+"位置不合法");
        }
        this.array[pos] = value;
    }
    //删除第一次出现的目标元素
    public void del(int toDel){
        int dex = indexOf(toDel);
        if (dex == -1){
            System.out.println("没有要删除的元素");
            return;
        }
        for (int i = dex; i < this.usedSize-1; i++) {
            this.array[i] = this.array[i+1];
        }
        //引用类型
        /*this.array[usedSize-1] = null;*/
        this.usedSize--;
    }
    //获取顺序表长度
    public int size(){
        return this.usedSize;
    }
    //清空顺序表
    public void clear(){
        //引用类型
        /*for (int i = 0; i < this.usedSize; i++) {
            this.array[i] = null;
        }*/
        this.usedSize = 0;
    }
}



public class Main {
    public static void main(String[] args) {
       MyArrayList<Integer> myArrayList = new MyArrayList<>();
       myArrayList.add(1);
       myArrayList.add(1,2);
       myArrayList.show();
    }
}